package com.web.combo.action;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.web.combo.bean.TreeNode;
import com.web.combo.service.ComboboxService;
import com.web.combo.service.impl.ComboboxServiceImpl;

@SuppressWarnings("serial")
public class ComboboxAction extends ActionSupport {

	private Map<String, Object> result;

	private List<TreeNode> nodes;

	private ComboboxService service;

	public ComboboxAction() {
		this.result = new HashMap<String, Object>();
		this.service = new ComboboxServiceImpl();
	}

	public String getCombobox() throws SQLException, IOException {
		this.result.put("result", this.service.getCombobox());
		return SUCCESS;
	}

	public String getCombolist() throws SQLException, IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));

		this.result.put("result", this.service.getCombobox(start, limit));
		this.result.put("total", this.service.getComboboxCount());
		return SUCCESS;
	}

	public String getCombotree() throws SQLException, IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("node");

		this.nodes = this.service.getCombotree(id);
		return SUCCESS;
	}

	public Map<String, Object> getResult() {
		return result;
	}

	public void setResult(Map<String, Object> result) {
		this.result = result;
	}

	public List<TreeNode> getNodes() {
		return nodes;
	}

	public void setNodes(List<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public ComboboxService getService() {
		return service;
	}

	public void setService(ComboboxService service) {
		this.service = service;
	}

}
